#pragma once




#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#if defined(_WIN32) && !defined(__cplusplus)
#define inline __inline
#endif

#define max(a,b)    (((a) > (b)) ? (a) : (b))

#define BOOL	int 
#define TRUE	1
#define FALSE	0

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

inline const char * GetReverseStr(const char *source)
{
	int len = strlen(source);
	char * result = (char *)malloc(sizeof(char) * len + 1);

	for (int i = 0; i < len; i++)
	{
		result[i] = source[len - 1 - i];
	}
	result[len] = 0;
	return result;
}

inline void Reverse(char *source)
{
	int len = strlen(source);
	for (int i = 0; i < len / 2; i++)
	{
		char temp = source[i];
		source[i] = source[len - 1 - i];
		source[len - 1 - i] = temp;
	}
}

inline const char * plus(const char *num1, const char *num2)
{
	int lengthA = strlen(num1);
	int lengthB = strlen(num2);

	int length = max(lengthA, lengthB);

	char *a = (char *)malloc(sizeof(char) * length + 1);
	char *b = (char *)malloc(sizeof(char) * length + 1);

	for (int i = 0; i < lengthA; i++)
	{
		a[i] = num1[lengthA - 1 - i];
	}
	for (int i = lengthA; i < length; i++)
	{
		a[i] = '0';
	}
	for (int i = 0; i < lengthB; i++)
	{
		b[i] = num2[lengthB - 1 - i];
	}
	for (int i = lengthB; i < length; i++)
	{
		b[i] = '0';
	}
	char *c = (char *)malloc(sizeof(char) * length + 2);

	int carry = 0;
	for (int i = 0; i < length; i++)
	{
		int p = (a[i] - 48) + (b[i] - 48) + carry;//上次的进位
		c[i] = p % 10 + 48;
		carry = p / 10;
	}
	c[length] = carry + 48;
	c[length + 1] = 0;

	free(a);
	free(b);

	Reverse(c);
	for (int i = 0; i < length + 2; i++)//移除串首的0
	{
		if (c[i] != '0')
		{
			if (c[i] == 0)
			{
				c = &c[i - 1];
			}
			else
			{
				c = &c[i];
				break;
			}
		}
	}
	return c;
}

// 此处需要满足a >= b
inline const char * minus(const char *num1, const char *num2)
{
	int lengthA = strlen(num1);
	int lengthB = strlen(num2);

	int length = max(lengthA, lengthB);

	char *a = (char *)malloc(sizeof(char) * length + 1);
	char *b = (char *)malloc(sizeof(char) * length + 1);

	for (int i = 0; i < lengthA; i++)
	{
		a[i] = num1[lengthA - 1 - i];
	}
	for (int i = lengthA; i < length; i++)
	{
		a[i] = '0';
	}
	for (int i = 0; i < lengthB; i++)
	{
		b[i] = num2[lengthB - 1 - i];
	}
	for (int i = lengthB; i < length; i++)
	{
		b[i] = '0';
	}

	char *c = (char *)malloc(sizeof(char) * length + 1);
	int borrow = 0;
	for (int i = 0; i < length; i++)
	{
		int p = (a[i] - 48) - (b[i] - 48) - borrow;//上次的进位
		if (p < 0)
		{
			borrow = 1;
			p += 10;
		}
		else borrow = 0;
		c[i] = p % 10 + 48;

	}
	c[length] = 0;

	free(a);
	free(b);

	Reverse(c);
	for (int i = 0; i < length + 1; i++)//移除串首的0
	{
		if (c[i] != '0')
		{
			if (c[i] == 0)
			{
				c = &c[i - 1];
			}
			else
			{
				c = &c[i];
				break;
			}
		}
	}
	return c;
}

// 此处需要满足1 <= n <= 9
inline const char * mutiply(const char *source, int n)
{
	int length = strlen(source);
	char * result = (char *)malloc(sizeof(char) * length + 2);
	char *a = (char *)GetReverseStr(source);
	int i;
	int carry = 0;
	char p = 0;
	for (i = 0, carry = 0; i < length; i++)
	{
		p = (a[i] - 48) * n + carry;//上次的进位
		result[i] = p % 10 + 48;
		carry = p / 10;
	}
	result[length] = carry + 48;
	result[length + 1] = 0;

	free(a);
	Reverse(result);
	for (int i = 0; i < length + 2; i++)//移除串首的0
	{
		if (result[i] != '0')
		{
			if (result[i] == 0)
			{
				result = &result[i - 1];
			}
			else
			{
				result = &result[i];
				break;
			}
		}
	}
	return result;
}

inline const char * GetNumOf9(const char *a)
{
	int lengthA = strlen(a);
	char * result = (char *)malloc(sizeof(char) * lengthA + 1);

	const char *p = a;
	int indexOfFirst9 = 0;
	for (int i = 0; i < lengthA; i++)
	{
		if (a[i] == '9')
		{
			indexOfFirst9 = i; // 找到最高位的9
		}
	}

	int lengthARemain = lengthA - strlen(&a[indexOfFirst9]); // 总长度减去最高位9的位置	
	for (int i = 1; i <= indexOfFirst9; i++)
	{

	}

	return "";
}
